//
//  WenXianCanKaoListViewController.m
//  MyYiJianKangApp
//
//  Created by 张国文 on 16/5/12.
//  Copyright © 2016年 hangzhouyijiyin. All rights reserved.
//

#import "WenXianCanKaoListViewController.h"


#define kCurrentUserToken [[NSUserDefaults standardUserDefaults]objectForKey:@"token"]

@interface WenXianCanKaoListViewController ()

@property (nonatomic, strong) UIScrollView *backGroundView; // 背景

@end

@implementation WenXianCanKaoListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    // navi的修改
    [self changeNavigation];
    // 添加数据源
    [self addData];
  
}

// navi的修改
- (void)changeNavigation
{
    self.navigationItem.title = @"文献参照表";
    UIImageView *leftImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
    leftImageView.image = [UIImage imageNamed:@"houtui"];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftImageView];
    UITapGestureRecognizer *leftTapGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(lastPage:)];
    [leftImageView addGestureRecognizer:leftTapGes];
    
}

#pragma mark - 返回上一页面
- (void)lastPage:(UITapGestureRecognizer *)tapGes
{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - 添加数据源
- (void)addData
{

    if (self.isShiLiBaoGao) {
//        self.wenXianArray = [NSMutableArray arrayWithObjects:@"Lu X, Wang L, Chen S, et al. Genome-wide association study in Han Chinese identifies four new susceptibility loci for coronary artery disease.[J]. Nature Genetics, 2012, 44(8):890-4.", @"Hinohara K, Nakajima T, Takahashi M, et al. Replication of the association between a chromosome 9p21 polymorphism and coronary artery disease in Japanese and Korean populations[J]. Journal of Human Genetics, 2008, 53(4):357-9.", @"Willer C J, Sanna S, Jackson A U, et al. Newly identified loci that influence lipid concentrations and risk of coronary artery disease.[J]. Nature Genetics, 2008, 40(2):161-9.", @"Schunkert H, König I R, Kathiresan S, et al. Large-scale association analysis identifies 13 new susceptibility loci for coronary artery disease[J]. Nature Genetics, 2011, 43(4):333-338.", @"Gu Y, Liu Z, Li L, et al. OLR1, PON1 and MTHFR gene polymorphisms, conventional risk factors and the severity of coronary atherosclerosis in a Chinese Han population.[J]. Cellular Physiology & Biochemistry, 2013, 31(1):143-152.", @"an Wang, et al. Genome-wide association identifies a susceptibility locus for coronary artery disease in the Chinese Han population.Nature Genetics, doi:10.1038/ng.783", @"Shen G Q, Li L, Rao S, et al. Four SNPs on chromosome 9p21 in a South Korean population implicate a genetic locus that confers high cross-race risk for development of coronary artery disease.[J]. Arteriosclerosis Thrombosis & Vascular Biology, 2008, 28(2):360-5.", nil];
        // 布局
        [self addMySubview];
    } else {
        [[JiaZaiAnimationHandle shareJiaZaiAnimationHandle] startAnimation];
        // 基因参考文献url
        NSString *geneWenXianUrl = [NSString stringWithFormat:@"%@?token=%@", kZGWGetGeneWenXian, kCurrentUserToken];
        
        NSString *positionString = [NSString stringWithFormat:@"chr%@_%@", self.jiYinJianCeJieGuoModel.chrom, self.jiYinJianCeJieGuoModel.rs_pos];
        NSDictionary *paramatic = @{@"position":positionString, @"base_change":self.jiYinJianCeJieGuoModel.gene_type};
        AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
        [manager GET:geneWenXianUrl parameters:paramatic success:^(NSURLSessionDataTask *task, id responseObject) {
            [[JiaZaiAnimationHandle shareJiaZaiAnimationHandle] stopAnimation];
            if (!responseObject) {
                NSLog(@"走了");
                return;
            }
            NSArray *literaturesArray = [responseObject valueForKey:@"literatures"];
            self.wenXianArray = [NSMutableArray array];
            for (NSString *tempString in literaturesArray) {
                [self.wenXianArray addObject:tempString];
            }
            // 布局
            [self addMySubview];
        } failure:^(NSURLSessionDataTask *task, NSError *error) {
            [[JiaZaiAnimationHandle shareJiaZaiAnimationHandle] stopAnimation];
            [[JiaZaiAnimationHandle shareJiaZaiAnimationHandle] addAlertTitle:@"提示" Content:@"数据获取失败，请返回上一页重新进入" ToView:self];
        }];
    }
    
    
}

#pragma mark - 布局
- (void)addMySubview
{
//    CGFloat nextWenXianHeight = 0.0;
//    for (int i = 0; i < self.wenXianArray.count; i++) {
//        UIButton *wenXianButton = [UIButton buttonWithType:UIButtonTypeSystem];
//        wenXianButton.frame = CGRectMake((375 - 150) / 2 * kAdjustParamater, 45 * kAdjustParamater + nextWenXianHeight, 150 * kAdjustParamater, 40 * kAdjustParamater);
//        wenXianButton.backgroundColor = kCyanColor;
//        [wenXianButton setTitle:self.wenXianArray[i] forState:UIControlStateNormal];
//        [wenXianButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
//        wenXianButton.titleLabel.font = [UIFont fontWithName:@"Arial-BoldMT" size:k18Big];
//        [wenXianButton addTarget:self action:@selector(wenXian:) forControlEvents:UIControlEventTouchUpInside];
//        wenXianButton.tag = 200 + i;
//        wenXianButton.layer.cornerRadius = 5 * kAdjustParamater;
//        wenXianButton.layer.masksToBounds = YES;
//        [self.view addSubview:wenXianButton];
//        nextWenXianHeight = nextWenXianHeight + wenXianButton.height + 25 * kAdjustParamater;
//    }
    if (self.wenXianArray.count == 0) {
        // 说明没有参考文献
        UILabel *alertLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 250 * kAdjustParamater, kScreenWidth, 30 * kAdjustParamater)];
        alertLabel.text = @"该基因目前没有参考文献";
        alertLabel.textAlignment = NSTextAlignmentCenter;
        alertLabel.textColor = [[UIColor blackColor] colorWithAlphaComponent:0.3];
        alertLabel.font = [UIFont fontWithName:@"Arial-BoldMT" size:25 * kAdjustParamater];
        [self.view addSubview:alertLabel];
    } else {
        self.backGroundView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
        self.backGroundView.backgroundColor = [UIColor whiteColor];
        self.backGroundView.bounces = NO;
        self.backGroundView.showsVerticalScrollIndicator = NO;
        self.backGroundView.contentSize = CGSizeMake(kScreenWidth, 1.1 * kAdjustParamater);
        [self.view addSubview:_backGroundView];
        
        // 记录下一个文献的开始位置
        CGFloat nextWenXianStartY = 0.0;
        
        // 进行动态的布局
        for (int i = 0; i < self.wenXianArray.count; i++) {
            // 文献前方的数字
            UILabel *wenXianNumberLabel = [[UILabel alloc] initWithFrame:CGRectMake(20 * kAdjustParamater, 30 * kAdjustParamater + nextWenXianStartY, 60 * kAdjustParamater, 25 * kAdjustParamater)];
            wenXianNumberLabel.backgroundColor = kCyanColor;
            wenXianNumberLabel.text = [NSString stringWithFormat:@"文献%d", i + 1];
            wenXianNumberLabel.textAlignment = NSTextAlignmentCenter;
            wenXianNumberLabel.textColor = [UIColor whiteColor];
            wenXianNumberLabel.font = [UIFont fontWithName:@"Arial-BoldMT" size:k16Big];
            wenXianNumberLabel.layer.cornerRadius = 5 * kAdjustParamater;
            wenXianNumberLabel.layer.masksToBounds = YES;
            [self.backGroundView addSubview:wenXianNumberLabel];
            
            
            
            // 文献内容表
            UILabel *wenXianContentLabel = [[UILabel alloc] initWithFrame:CGRectMake(20 * kAdjustParamater, wenXianNumberLabel.bottom + 15 * kAdjustParamater, 340 * kAdjustParamater, 100 * kAdjustParamater)];
            //  wenXianContentLabel.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.1];
            wenXianContentLabel.text = self.wenXianArray[i];
            wenXianContentLabel.textColor = [UIColor blackColor];
            wenXianContentLabel.font = [UIFont systemFontOfSize:k16Big];
            wenXianContentLabel.numberOfLines = 0;
            wenXianContentLabel.tag = 100 + i;
            // 调整行间距
            [wenXianContentLabel setAttributedText:[[NetStatusHandle shareHandle] setHangJianJvOfLabel:wenXianContentLabel.text lineHeight:10]];
            [wenXianContentLabel sizeToFit];
            [self.backGroundView addSubview:wenXianContentLabel];
            
            // 文献数字下方的灰色条
            UILabel *grayLineOnWenXianNumberLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, wenXianContentLabel.bottom + 15 * kAdjustParamater, kScreenWidth, 10 * kAdjustParamater)];
            grayLineOnWenXianNumberLabel.backgroundColor = [[UIColor blackColor ] colorWithAlphaComponent:0.05];
            [self.backGroundView addSubview:grayLineOnWenXianNumberLabel];
            
            nextWenXianStartY = nextWenXianStartY + 15 * kAdjustParamater + 25 * kAdjustParamater + 10 * kAdjustParamater + wenXianContentLabel.height + 15 * kAdjustParamater + 15 * kAdjustParamater;
            
        }
        if (self.isShiLiBaoGao == YES) {
            // 取出最后一个文献
            UILabel *contentLabel = (UILabel *)[self.view viewWithTag:99 + self.wenXianArray.count];
            self.backGroundView.contentSize = CGSizeMake(kScreenWidth, contentLabel.bottom + 50 * kAdjustParamater);
        } else {
            // 取出最后一个文献
            UILabel *contentLabel = (UILabel *)[self.view viewWithTag:99 + self.wenXianArray.count];
            self.backGroundView.contentSize = CGSizeMake(kScreenWidth, contentLabel.bottom + 100 * kAdjustParamater);
        }
        

    }
    
}

//#pragma mark -  点击文献
//- (void)wenXian:(UIButton *)button
//{
//    NSLog(@"点击了第%ld个文献", button.tag - 199);
//}






- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
